package com.cloud.pora.mrm.web;

import java.util.List;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.log.Log;

import com.cloud.pora.mrm.model.MeetingRoom;
import com.cloud.pora.mrm.model.MeetingRoomBooking;
import com.cloud.pora.mrm.web.common.util.CheckUtil;

@Name("meetingRoomBookingAction")
public class MeetingRoomBookingAction {
    @Logger
    private Log log;

    // @In
    @Out(required = false)
    @In(required = false)
    private MeetingRoomBooking meetingRoomBooking;

    @In
    EntityManager entityManager;

    @In
    private MeetingRoomBookingDto meetingRoomBookingDto;
    @In
    private MeetingRoomDto meetingRoomDto;

    private MeetingRoom meetingRoom;

    public MeetingRoom getMeetingRoom() {
        return meetingRoom;
    }

    public void setMeetingRoom(MeetingRoom meetingRoom) {
        this.meetingRoom = meetingRoom;
    }


    public MeetingRoomBookingDto getMeetingRoomBookingDto() {
        return meetingRoomBookingDto;
    }

    public void setMeetingRoomBookingDto(MeetingRoomBookingDto meetingRoomBookingDto) {
        this.meetingRoomBookingDto = meetingRoomBookingDto;
    }

    public MeetingRoomDto getMeetingRoomDto() {
        return meetingRoomDto;
    }
    
    public void setMeetingRoomDto(MeetingRoomDto meetingRoomDto) {
        this.meetingRoomDto = meetingRoomDto;
    }

    // public void page() {
    // this.meetingRoomBooking.setMeetingRoom(this
    // .findMeetingRoom(meetingRoomBookingDto.getMrId()));
    // }
    //
    // private MeetingRoom findMeetingRoom(Long mrId) {
    // return (MeetingRoom) entityManager
    // .createQuery(
    // "select mr from MeetingRoom mr where mr.mrId=:mrId")
    // .setParameter("mrId", mrId).getSingleResult();
    // }

    public void page() {

        if (meetingRoomBookingDto.getMrbId() != null) {
            this.meetingRoomBooking = this
                    .findMeetingRoomBooking(meetingRoomBookingDto.getMrbId());
        } else if (meetingRoomDto.getMrId() != null) {

            // ??
            this.meetingRoomBooking = new MeetingRoomBooking();

            this.meetingRoomBooking.setMeetingRoom(this
                    .findMeetingRoom(meetingRoomDto.getMrId()));

            this.meetingRoomBooking.setMeetingStartTime(meetingRoomBookingDto
                    .getMeetingStartTime());

            this.meetingRoomBooking.setMeetingEndTime(meetingRoomBookingDto
                    .getMeetingEndTime());

        }

    }

    private MeetingRoom findMeetingRoom(Long mrId) {
        return (MeetingRoom) entityManager
                .createQuery(
                        "select mr from MeetingRoom mr where mr.mrId=:mrId")
                .setParameter("mrId", mrId).getSingleResult();
    }

    private MeetingRoomBooking findMeetingRoomBooking(Long mrbId) {
        return (MeetingRoomBooking) entityManager
                .createQuery(
                        "select mrb from MeetingRoomBooking mrb where mrb.mrbId=:mrbId")
                .setParameter("mrbId", mrbId).getSingleResult();
    }

    public String create() {
        if (!this.checkBooking()) {
            addErrorMessage("此时间段已被预约!");
            return null;

        } else {
            meetingRoomBooking.setInsertId("admin");
            entityManager.persist(meetingRoomBooking);
            return "create_success";
        }

    }

    public String update() {
        meetingRoomBooking.setUpdateId("admin");
        return "update_success";
    }

    public String delete() {
        entityManager.remove(meetingRoomBooking);
        return "delete_success";
    }

    public String booking() {
        if (!this.checkBooking()) {
            addErrorMessage("此时间段已被预约!");
            return null;

        } else {
            create();
            return "booking_success";
        }
    }

    protected void addErrorMessage(String errorMsg) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMsg, ""));
    }

    public boolean checkBooking() {

        /* create query */
        StringBuffer queryString = new StringBuffer();
        queryString
                .append("select distinct mrb from MeetingRoomBooking mrb,MeetingRoom mr where mrb.meetingRoom.mrId=mr.mrId ");
        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrName())) {
            queryString.append(" and mr.mrName=:mrName");
        }

        queryString.append(" and (");
        queryString.append(" (");
        queryString
                .append(" mrb.meetingStartTime BETWEEN :meetingStartTime AND :meetingEndTime");
        queryString
                .append(" and mrb.meetingEndTime BETWEEN :meetingStartTime AND :meetingEndTime");
        queryString.append(" )");
        queryString.append(" or (");
        queryString
                .append(" :meetingStartTime BETWEEN mrb.meetingStartTime AND mrb.meetingEndTime");
        queryString
                .append(" and :meetingEndTime BETWEEN mrb.meetingStartTime AND mrb.meetingEndTime");

        queryString.append(" and mrb.meetingStartTime > now()");
        queryString.append(" )");
        queryString.append(" )");

        /* Executed query */
        Query query = entityManager.createQuery(queryString.toString());

        if (!CheckUtil.isEmpty(meetingRoomBooking.getMeetingRoom().getMrName())) {
            query.setParameter("mrName", meetingRoomBooking.getMeetingRoom()
                    .getMrName());
        }

        query.setParameter("meetingStartTime",
                meetingRoomBooking.getMeetingStartTime());
        query.setParameter("meetingEndTime",
                meetingRoomBooking.getMeetingEndTime());

        List<MeetingRoomBooking> mrbs = query.getResultList();

        return mrbs == null || mrbs.size() == 0;
    }
    
    public TimeZone getTimeZone() {
//        return TimeZone.getTimeZone("GMT+8");
        return TimeZone.getTimeZone("CCT");
    }
}
